<?php
function download($file_dir,$file_name,$filename2="")
{
	if ($filename2=="") {
		$filename2=$file_name;
	}
	$file_dir = chop($file_dir);
	if($file_dir != '')
	{
		$file_path = $file_dir;
		if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
		$file_path .= '/';
		$file_path .= $file_name;
	}
	else
	$file_path = $file_name;

	$r_url = explode('/',$_SERVER['REQUEST_URI']);
	$file_path = 'http://'.$_SERVER['HTTP_HOST']."/".$r_url[1]."/".$file_dir."/".$file_name;
	if(!@fopen($file_path,"r"))
	{
		echo '对不起,你要下载的文件不存在。';
		return false;
	}
	//echo "<script type='text/javascript'>location.href='".$file_path."';</script>";
	header("Location: ".$file_path);
	exit('文件下载完成！');
	if(!file_exists($file_path))
	{
		echo '对不起,你要下载的文件不存在。';
		return false;
	}
	$file_size = filesize($file_path);
	header("Content-type: application/octet-stream");
	header("Accept-Ranges: bytes");
	header("Accept-Length: $file_size");
	header("Content-Disposition: attachment; filename=".$filename2);
	$fp = fopen($file_path,"r");
	$buffer_size = 1024;
	$cur_pos = 0;

	while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
	{
		$buffer = fread($fp,$buffer_size);
		echo $buffer;
		$cur_pos += $buffer_size;
	}
	$buffer = fread($fp,$file_size-$cur_pos);
	echo $buffer;
	fclose($fp);
	return true;
}
download($dir,$filename,$filename2="");
?>
